/**
 * chinagoods.com
 * Copyright (c) 2020-2024 All Rights Reserved.
 */
package com.ruoyi.web.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author yangpeng
 * Created by on 2024-01-24 17:18
 */
@Api(tags = "开放接口---代付订单")
@RestController
@RequestMapping("/api/chinagoods/")
@Slf4j
public class ChinagoodsController {


    @ApiOperation(value = "回调支付", hidden = true)
    @RequestMapping("/notifyOrder")
    public void notifyOrder(@RequestBody JSONObject map) throws Exception {
        log.info("回调参数:{}", JSONObject.toJSONString(map));
        String url = "https://testapiserver.chinagoods.com/bt/api/v1/mini/pay/notifyOrder";
        RequestUtil.postJson(url, map);
    }

    @ApiOperation(value = "回调支付", hidden = true)
    @RequestMapping("/notifyOrder1")
    public void notifyOrder(HttpServletRequest request) throws Exception {

        log.info("回调参数:{}", getRequestBody(request));


    }

    public static String getRequestBody(HttpServletRequest request) throws IOException {
        ServletInputStream stream;
        BufferedReader reader = null;
        StringBuffer sb = new StringBuffer();
        try {
            stream = request.getInputStream();
            // 获取响应
            reader = new BufferedReader(new InputStreamReader(stream));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            throw new IOException("读取返回支付接口数据流出现异常！");
        } finally {
            reader.close();
        }
        return sb.toString();
    }

}